//
//  SCBaseViewController.m
//  Shuaiche
//
//  Created by dangshuai on 2018/12/18.
//  Copyright © 2018 zzs. All rights reserved.
//

#import "SCBaseViewController.h"

@interface SCBaseViewController ()

@end

@implementation SCBaseViewController

- (UIStatusBarStyle)preferredStatusBarStyle {
  return UIStatusBarStyleDefault;
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  self.edgesForExtendedLayout = UIRectEdgeNone;
  
  if (@available(iOS 15.0, *)) {
      UINavigationBarAppearance *navigationBarAppearance = [UINavigationBarAppearance new];
      navigationBarAppearance.backgroundColor = [UIColor whiteColor];
      navigationBarAppearance.backgroundEffect = nil;
      navigationBarAppearance.shadowColor = [UIColor colorWithHexString:LIGHT_GRAY_LINE_COLOR];
      self.navigationController.navigationBar.scrollEdgeAppearance = navigationBarAppearance;
      self.navigationController.navigationBar.standardAppearance = navigationBarAppearance;
  }
  if (self.hideNavigationBar) {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
  } else {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
  }
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  
  NSArray *viewControllers = self.navigationController.viewControllers;
  if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) {
    // View is disappearing because a new view controller was pushed onto the stack
    if (self.navigationController.navigationBar.hidden) {
      [self.navigationController setNavigationBarHidden:YES animated:animated];
    } else {
      [self.navigationController setNavigationBarHidden:NO animated:animated];
    }
  } else if ([viewControllers indexOfObject:self] == NSNotFound) {
    // View is disappearing because it was popped from the stack
    if (self.navigationController.navigationBar.hidden || (viewControllers.count == 1 && ![viewControllers[0] isKindOfClass:[SCBaseViewController class]])) {//后面的或：代表
      [self.navigationController setNavigationBarHidden:YES animated:animated];
    } else {
      [self.navigationController setNavigationBarHidden:NO animated:animated];
    }
  }
}

- (void)viewDidLoad {
    [super viewDidLoad];
  self.view.backgroundColor = [UIColor colorWithHexString:APP_BACKGROUNDCOLOR];
  
  if (CGRectIsEmpty(self.frame)) {
      self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, WINDOW_WIDTH, WINDOW_HEIGHT-(STATUSBAR_HEIGHT+NAVIGATIONBAR_HEIGHT));
  } else {
      self.view.frame = self.frame;
  }
  NSLog(@"self.navigationController.viewControllers.count = %ld",self.navigationController.viewControllers.count);
  if (self.navigationController.viewControllers.count > 1) {
    backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 60, 40);
    
    [backBtn setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backEvent) forControlEvents:UIControlEventTouchUpInside];
    _leftBarItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = _leftBarItem;
    
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -45, 0, 0);
  }
  
  NSDictionary *navTitleArr = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:NAVIGATIONBAR_FONT],NSFontAttributeName,[UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1],NSForegroundColorAttributeName,nil];
  
  [self.navigationController.navigationBar setTitleTextAttributes:navTitleArr];
}

#pragma mark - backEvent
-(void)backEvent {
  [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    if (!CGRectIsEmpty(self.frame)) {
        self.view.frame = self.frame;
    }
}

- (void)popGestureEnable:(BOOL)enable {
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = enable;
    }
}

- (void)dealloc {
  NSString  *ctrl_controller = NSStringFromClass([self class]);
  //    NSLog(@"释放 控制器类型:%@",ctrl_controller);
  LogInfo(@"释放 控制器类型:%@",ctrl_controller);
}

@end
